﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiskInventory
{
    public partial class DiskInventoryDGV : Form
    {
        private Environment.SpecialFolder[] Specials = new Environment.SpecialFolder[] 
        {
            Environment.SpecialFolder.AdminTools,
            Environment.SpecialFolder.ApplicationData,
            Environment.SpecialFolder.CDBurning,
            Environment.SpecialFolder.CommonAdminTools,
            Environment.SpecialFolder.CommonProgramFiles,
            Environment.SpecialFolder.CommonProgramFilesX86,
            Environment.SpecialFolder.CommonPrograms,
            Environment.SpecialFolder.CommonStartMenu,
            Environment.SpecialFolder.CommonStartup,
            Environment.SpecialFolder.CommonTemplates,
            Environment.SpecialFolder.Cookies,
            Environment.SpecialFolder.Favorites,
            Environment.SpecialFolder.Fonts,
            Environment.SpecialFolder.History,
            Environment.SpecialFolder.InternetCache,
            Environment.SpecialFolder.LocalizedResources,
            Environment.SpecialFolder.PrinterShortcuts,
            Environment.SpecialFolder.ProgramFiles,
            Environment.SpecialFolder.ProgramFilesX86,
            Environment.SpecialFolder.Programs,
            Environment.SpecialFolder.Recent,
            Environment.SpecialFolder.Resources,
            Environment.SpecialFolder.SendTo,
            Environment.SpecialFolder.StartMenu,
            Environment.SpecialFolder.Startup,
            Environment.SpecialFolder.System,
            Environment.SpecialFolder.SystemX86,
            Environment.SpecialFolder.Templates,
            Environment.SpecialFolder.UserProfile,
            Environment.SpecialFolder.Windows
        };
        private List<string> ExcludeFolders;
        public DiskInventoryDGV()
        {
            InitializeComponent();
            ExcludeFolders = Specials.Select(qF => Environment.GetFolderPath(qF)).Where(qR => !string.IsNullOrEmpty(qR)).Distinct().ToList();
            this.diskInventoryDataSet1.Load();
        }
        private string[] GetDirectories(string aPath)
        {
            string[] Result = new string[0];
            try
            {
                Result = System.IO.Directory.GetDirectories(aPath);
            }
            catch
            {
            }
            return Result;
        }
        private string[] ListFolders(string aPath)
        {
            if (this.ExcludeFolders.Contains(aPath)) return new string[0];
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(aPath);
            if (di.Parent != null && di.Attributes.HasFlag(System.IO.FileAttributes.System)) return new string[0];
            List<string> Result = new List<string>() { aPath };
            foreach (string Folder in GetDirectories(aPath))
                Result.AddRange(ListFolders(Folder));
            return Result.ToArray();
        }
        private void UpdateToolStripButton_Click(object sender, EventArgs e)
        {
            DiskInventoryDataSet NewSet = new DiskInventoryDataSet();
            foreach (string Drive in System.IO.DriveInfo.GetDrives()
                .Where(qD => qD.DriveType == System.IO.DriveType.Fixed && qD.IsReady)
                .Select(qD => qD.Name))
            {
                foreach(string Path in ListFolders(Drive))
                {
                    try
                    {
                        string[] Files = System.IO.Directory.GetFiles(Path);
                        if (Files.Length > 0)
                        {
                            DiskInventoryDataSet.DiskInventoryRow oRow = this.diskInventoryDataSet1.DiskInventory
                                .FindByPath(Path);
                            NewSet.DiskInventory.AddDiskInventoryRow(Path, ((oRow == null) ? string.Empty : oRow.Description));
                        }
                    }
                    catch
                    {
                    }
                }
            }
            this.diskInventoryDataSet1.Dispose();
            this.diskInventoryDataSet1 = NewSet;
            this.diskInventoryDataSet1.Save();
            this.dataGridView1.AutoGenerateColumns = false;
            this.diskInventoryBindingSource.DataSource = this.diskInventoryDataSet1;
            this.dataGridView1.Refresh();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex <= 0) return;
            if(e.ColumnIndex == this.TabsCol.Index)
            {
                int Levels = ((string)e.Value).Where(qC => qC == '\\').Count();
                e.Value = new string(' ', Levels) + e.Value; // System.IO.Path.GetDirectoryName((string)e.Value); // new string('-', 40 - Levels);
                e.FormattingApplied = true;
            }
            else if (e.ColumnIndex == this.FolderCol.Index)
            {
                e.Value = System.IO.Path.GetFileName((string)e.Value);
                e.FormattingApplied = true;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(DiskInventoryDataSet.DefaultPath());
        }
    }
}
